package api;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2024-03-21
 * Time: 18:01
 */
//实现注销功能
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if(session == null) {
            // 说明本身就为 未登录状态 因此谈不上退出登录
            resp.sendRedirect("login.html");
            return;
        }
        // 之前登录成功后，就会给 session 中存储 user 这样的 Attribute.
        // 把这个删掉之后，自然就会判定为“未登录”了。
        session.removeAttribute("user");
        resp.sendRedirect("login.html");
    }
}
